package com.blb.java11student.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.java11student.entity.ResponseResult;
import com.blb.java11student.entity.Student;
import com.blb.java11student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

//@CrossOrigin
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询
     */
    @GetMapping("/students-page")
    public ResponseResult<Page<Student>> getStudentPage(@RequestParam(required = false,defaultValue = "1") Long current,
                                                        @RequestParam(required = false,defaultValue = "10") Long size){
        Page<Student> page = studentService.getStudentPage(current, size);
        return ResponseResult.ok(page);
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @PostMapping("/student")
    public ResponseResult<String> addStudent(@RequestBody Student student){
        studentService.save(student);
        return ResponseResult.ok(student);
    }

    /**
     * 更新
     * @param student
     * @return
     */
    @PutMapping("/student")
    public ResponseResult<String> updateStudent(@RequestBody Student student){
        studentService.updateById(student);
        return ResponseResult.ok(student);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/student/{id}")
    public ResponseResult<String> deleteStudent(@PathVariable Long id){
        studentService.removeById(id);
        return ResponseResult.ok(id);
    }
}
